//
//  DQMeCell.m
//  DQBaisibudejie
//
//  Created by 杜倩 on 16/9/2.
//  Copyright © 2016年 杜倩. All rights reserved.
//

#import "DQMeCell.h"
#import "DQCollectionCell.h"
#import <AFNetworking.h>
#import "DQMeModel.h"
#import "DQLoginViewController.h"
@implementation DQMeCell
static NSString *ID=@"cell";

#pragma mark 懒加载-申请空间
-(NSMutableArray *)arrM{
    if (_arrM==nil) {
        _arrM=[NSMutableArray array];
    }
    return _arrM;
}

#pragma mark 定义collectionview
-(void)addCollectionView{
    
}

#pragma mark 每组多少个collectionvire
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrM.count;//2
}

#pragma mark 定义cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //取模型
    DQMeModel *model=self.arrM[indexPath.row];//2
    DQCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.model=model;//2
    
    return cell;
}
#pragma Mark xib加载完数据自动调用
- (void)awakeFromNib {
    //代理
    _collectionView.dataSource=self;//数据源代理
    _collectionView.delegate=self;

    //通过纯代码注册cell
//    [self.collectionView registerClass:<#(nullable Class)#> forCellWithReuseIdentifier:<#(nonnull NSString *)#>];
    
    // 通过xib注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"DQCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:ID];
    [self addloadData];//调用下载数据

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
//9.2
#pragma mark 请求数据--加载给collectionview赋值
-(void)addloadData{
    //定义管理者
    AFHTTPSessionManager *Manager=[AFHTTPSessionManager manager];
    NSString *url=@"http://d.api.budejie.com/op/square/bs0315-iphone-4.3/0-100.json";
    //请求数据
    [Manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //[responseObject writeToFile:@"/Users/DuQian/Desktop/wode.plist" atomically:YES];
       // NSLog(@"%@",responseObject);
        //通过key值去
        NSArray *array=responseObject[@"square_list"];
        //遍历
        for (NSDictionary *dict in array) {
            DQMeModel *model=[DQMeModel meModelWithDict:dict];
            [self.arrM addObject:model];
        }
        //输出组数
        // NSLog(@"%ld",_arrM.count);
        [self.collectionView reloadData];//2 请求完数据刷新
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark用来监听选中的那个ITEM--取值--loginViewController
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    //排行榜单击----如果不是url，而是string跳转到登录界面的方法
    DQLoginViewController *login=[[DQLoginViewController alloc] init];
    if (indexPath.row==0) {
        //获得根控制器--登录controller 实现跳转
        [[UIApplication sharedApplication].keyWindow.rootViewController presentModalViewController:login animated:YES];
    }else{
        //取模型呢-item对应的值
        DQMeModel *model=self.arrM[indexPath.row];//存在两个网址--连接图片和其他的网址，用自带的safari打开网址
        NSURL *url=[NSURL URLWithString:model.url];
        [[UIApplication sharedApplication] openURL:url];
    }
    
}

@end
